package cn.linran._03_oneway;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import sun.plugin2.message.Message;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.1创建消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("OneWay_topic");
        //1.2连接NameServer
        consumer.setNamesrvAddr("localhost:9876");
        //1.3消费者接收主题
        consumer.subscribe("OneWay_topic","*");
        //1.4注册监听器
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                //1.5打印接收消息
                for (MessageExt messageExt : list) {
                    System.out.println(new String(messageExt.getBody(), StandardCharsets.UTF_8));
                }
                //1.6返回值，单向发送不接收
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //1.7启动消费者
        consumer.start();
        System.out.println("消费者以启动。。。");
    }
}
